<?php 
namespace utils;

use think\Request;
use utils\Auth;

class Menu {
    private $nodes;
    private $subMenu;
    private $menu;
    private $_menu;
    private $_subMenu;
    private $link_key;
    private $menus = [];
    private $request;

    public function __construct() {
        $request = Request::instance();
        $this->request = $request;

        $nodes= [
            'admin/index' => ['name' => '管理首页', 'parent' => 'config'],
            'admin/customer_list' => ['name' => '会员管理', 'parent' => 'config'],
            'admin/customer_edit' => ['name' => '编辑会员', 'parent' => 'config'],
            'admin/log_operation_list' => ['name' => '操作日志', 'parent' => 'config'],
        ];

        //分组菜单
        $subMenu = [
            'config' => ['name' => '会员管理' , 'parent' => 'system'],
        ];

        //主菜单
        $menu =[
            'system' => ['link'=>'admin/index' , 'name'=>'会员模块'],
        ];

        $this->menus = [
            'nodes' => $nodes,
            'subMenu' => $subMenu,
            'menu' => $menu
        ];

        $this->init_menu();
    }

    public function init_menu(){
        $nodes = $this->menus['nodes'];
        $subMenu = $this->menus['subMenu'];
        $menu = $this->menus['menu'];

        $manager = session('manager');
        if ($manager && isset($manager['is_admin']) && $manager['is_admin'] != 1) {
            $auth = Auth::instance();
            $rights = $auth->getAuthList($manager['id'], 1);

            if (is_array($nodes)) {
                $subMenuKey = [];
                foreach ($nodes as $key => $value) {
                    if (in_array($key, $rights)) {
                        if (!isset($subMenuKey[$value['parent']])) {
                            $subMenuKey[$value['parent']] = $key;
                        } else {
                            if (stristr($key,'_list')) {
                                $subMenuKey[$value['parent']] = $key;
                            }
                        }
                    } else {
                        unset($nodes[$key]);
                    }
                }

                $menuKey = [];
                foreach ($subMenu as $key => $value) {
                    if (isset($subMenuKey[$key])) {
                        $menuKey[$value['parent']] = $key;
                    } else {
                        unset($subMenu[$key]);
                    }
                }

                foreach ($menu as $key => $value) {
                    if (!isset($menuKey[$key])) {
                        unset($menu[$key]);
                    } else {
                        $menu[$key]['link'] = $subMenuKey[$menuKey[$key]];
                    }
                }
            }
        }

        if (is_array($nodes)) {
            $this->nodes = $nodes;
        } else {
            $this->nodes = [];
        }

        if (is_array($subMenu)) {
            $this->subMenu = $subMenu;
        } else {
            $this->subMenu = [];
        }

        if (is_array($menu)) {
            $this->menu = $menu;
        } else {
            $this->menu = [];
        }
        $this->_subMenu = [];
        $this->_menu = [];

        foreach ($this->nodes as $key => $nodes) {
            $this->_subMenu[$nodes['parent']][] = [
                'link' => $key,
                'name' => $nodes['name'],
                'hidden' => (isset($nodes['hidden']) && $nodes['hidden']) ? true : false
            ];
        }

        foreach ($this->subMenu as $key => $subMenu) {
            $this->_menu[$subMenu['parent']][] = [
                'link' => $key,
                'name' => $subMenu['name']
            ];
        }

        $this->link_key = ($this->request->controller() == null ? 'admin' : strtolower($this->request->controller())) . '/' . ($this->request->action() == null ? 'index' : $this->request->action());
    }

    public function getMenus(){
        return $this->menus;
    }

    public function setMenus($menus) {
        $this->menus = $menus;
    }

    public function current_menu($key = null) {
        $key = $this->link_key;
        if (isset($this->nodes[$key])) {
            $subMenu = $this->nodes[$key]['parent'];
            $menu = $this->subMenu[$subMenu]['parent'];
            return [
                'menu' => $menu,
                'subMenu' => $subMenu
            ];
        }
        return null;
    }

    public function getMenu() {
        return isset($this->menu) ? $this->menu : [];
    }

    public function getSubMenu($key) {
        return isset($this->_menu[$key]) ? $this->_menu[$key] : [];
    }

    public function getNodes($key) {
        return isset($this->_subMenu[$key]) ? $this->_subMenu[$key] : [];
    }

    public function currentNode() {
        $key = $this->link_key;
        return isset($this->nodes[$key]) ? $this->nodes[$key] : [];
    }

    public function setLink($link_key){
        $this->link_key = $link_key;
    }

    public function getLink() {
        return $this->link_key;
    }
}
